package service.impl;

import bean.Page;
import bean.Product;
import dao.impl.ProductDaoImpl;
import service.ProductService;

import java.util.ArrayList;
import java.util.List;

public class ProductServiceImpl implements ProductService {
    ProductDaoImpl productDao = new ProductDaoImpl();
    @Override
    public ArrayList<Product> queryAll() {

        return productDao.queryAll();
    }

    @Override
    public int deleteById(Integer id) {
        return productDao.delete(id);
    }

    @Override
    public int insert(Product product) {
        return productDao.insert(product);
    }

    @Override
    public int update(Product product) {
        return productDao.update(product);
    }

    @Override
    public Product queryById(Integer id) {
        return productDao.queryById(id);
    }

    @Override
    public Integer getPageTotalCount() {
        return productDao.queryForPageTotalCount();
    }

    @Override
    public Page<Product> queryByPage(Integer pageSize, Integer pageNo) {
        if (pageSize == null) {
            pageSize=Page.DEFAULT_PAGE_SIZE;
        }
        if (pageNo == null) {
            pageNo=1;
        }
        Page<Product> productPage = new Page<>(pageSize,pageNo);
        Integer pageTotalCount = getPageTotalCount();
        productPage.setTotalCount(pageTotalCount);
        productPage.setBegin((pageNo-1)*pageSize);
        int i = pageTotalCount / pageSize;
        productPage.setPageCount(pageTotalCount%pageSize==0? i:i+1);
        List<Product> products = productDao.queryForPageItems(productPage.getBegin(), productPage.getPageSize());
        productPage.setData(products);
        return productPage;
    }
}
